﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Deployment.Application;
using RapidSpider.Views;
using System.ComponentModel;

namespace RapidSpider.ApplicationControllers
{
    public partial class Controller
    {
        #region Members
        private UpdateNotificationView _UpdateNotificationView;
        #endregion

        #region Methods
        private void UpdateNotificationView_Show()
        {
            this._UpdateNotificationView = new UpdateNotificationView();

            this._UpdateNotificationView.LinkLabel.LinkClicked += 
                new LinkLabelLinkClickedEventHandler(UpdateNotificationView_LinkLabel_LinkClicked);
            this._UpdateNotificationView.timerCheckForUpdate.Tick += 
                new EventHandler(UpdateNotificationView_timerCheckForUpdate_Tick);
            this._UpdateNotificationView.timerHideStatusWindow.Tick += 
                new EventHandler(UpdateNotificationView_timerHideStatusWindow_Tick);
        }

        #region Event Handlers
        private void UpdateNotificationView_timerCheckForUpdate_Tick(object sender, EventArgs e)
        {
            Application.DoEvents();

            try
            {
                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    bool IsUpdateAvailable = false;
                    ApplicationDeployment theCurrentDeployment = ApplicationDeployment.CurrentDeployment;

                    IsUpdateAvailable =
                        theCurrentDeployment.CheckForUpdate();

                    if (IsUpdateAvailable)
                    {
                        this._UpdateNotificationView.ShowStatusWindow();
                    }
                }
            }
            catch { }
        }

        private void UpdateNotificationView_timerHideStatusWindow_Tick(
            object sender,
            EventArgs e)
        {
            this._UpdateNotificationView.HideStatusWindow();
        }

        private void UpdateNotificationView_LinkLabel_LinkClicked(
            object sender,
            LinkLabelLinkClickedEventArgs e)
        {
            Application.DoEvents();

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                ApplicationDeployment.CurrentDeployment.UpdateCompleted +=
                    new AsyncCompletedEventHandler(UpdateNotificationView_CurrentDeployment_UpdateCompleted);

                this._UpdateNotificationView.HideStatusWindow();

                ApplicationDeployment.CurrentDeployment.UpdateAsync();
            }
        }

        private void UpdateNotificationView_CurrentDeployment_UpdateCompleted(
            object sender,
            AsyncCompletedEventArgs e)
        {
            switch (MessageBox.Show(
                this._UpdateNotificationView,
                "The application has been upgraded.\n\nDo you want to restart now?",
                "Update Notification",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button1))
            {
                case DialogResult.No:
                    Application.DoEvents();
                    return;
            }

            Application.DoEvents();

            Application.Restart();
        }
        #endregion
        #endregion
    }
}
